#!/bin/bash
# Special thank to Shiroko, he give me the insight of coding in proper way

# Usage: $0 [arch]


################################# Here we set the configs
REALPATH=`realpath $0`
HERE_PATH=`dirname $REALPATH`
STORE_APP_PATH="$HERE_PATH"
WORKDIR=`mktemp -d`
ARCH="" # Define it later
ARCH_ANOTHERWAY="" # Define it later
TAR_GZ_DIR=""


PKG_NAME="包名"
APP_NAME="软件名"
#GITHUB_REPO="Fndroid/clash_for_windows_pkg" #格式像这样。只在从github获取信息才使用，如果不是，则注释掉。后续下载也整合进入helper
VERSION="版本号" # 定义版本号。或者，在后续prepare或build中定义。可使用其他两个bashimport文件中的功能
TAGS="community;ubuntu;deepin;uos" # 星火的tag信息
CATAGORY="network" #https://gitee.com/deepin-community-store/spark-store/blob/dev/DOCS/spk-doc.md
DETAIL="Clash For Windows 中文版，现在由Spark Building Service维护" # This should be written as json format
HOMEPAGE="主页网页"
MAINTAINER="名字<邮箱>"


################################# Fill the variables below only if you use write_control function
SECTION="utils"
PRIORITY="optional"
DEPENDS="依赖"
DESCRIPTION_SHORT="短介绍"
DESCRIPTION_LONG="长介绍"
PROVIDES=""
REPLACES=""
RECOMMENDS=""

################################# Here we import our tools
if [ ! -e "/opt/bashimport/sbs-utils.bashimport" ];then
echo "Need to install spark-building-service deb first! exit"
exit 1
fi
source /opt/bashimport/sbs-utils.bashimport
source /opt/bashimport/sbs-build-helper.bashimport


##这两个里面有详尽的解释
if [ "$1" = "amd64" ] || [ "$1" = "x64" ];then
ARCH="amd64"
ARCH_ANOTHERWAY="-amd64"
elif [ "$1" = "arm64" ] || [ "$1" = "arm" ];then
ARCH="arm64"
ARCH_ANOTHERWAY="-aarch64"
else
log.error "Err:Invalid Architecture"
exit 1
fi

# ===== Log 用法 =====
# log.info xxx
# log.warn xxx
# log.info xxx
# log.debug xxx
# 带颜色的echo

function prepare(){
log.info "Please to your prepare work here. "
# Below is the usable funcions

# aptss_get_update()：更新apt源并检查是否成功，如果成功则记录日志为“aptss update ok”，否则记录日志为“aptss is locked.Give up”并退出。

# get_version_from_aptss()：从aptss(全部)源中获取指定软件包的版本号。实战中建议使用下方的函数替代以避免无法预知的情况

# get_version_from_source_list_file ${PKG_NAME} ${PATH_TO_SOURCE_LIST}

# compare_version ${VER_Apt} ${VER_Upstream} (Will give the upstream version as variable $VERSION if it is higher or exit if they are the same or Ver_Apt is higher)

#    get_version_from_github()：从github上获取指定仓库的最新发布版本号。输入GITHUB_REPO作为参数，会返回版本号

#    get_version_from_gitee():和楼上一样


# get_size()：获取软件包目录的大小并返回值
}




function build(){

pushd $WORKDIR
# init_package_directory()：初始化软件包目录，创建DEBIAN、opt/apps/<PKG_NAME>/entries/applications、opt/apps/<PKG_NAME>/entries/icons和opt/apps/<PKG_NAME>/files等目录，并将模板应用到软件包目录中。

# write_control()：在软件包目录中生成control文件，并设置软件包的基本信息，如名称、版本、维护者、主页、架构、依赖关系、描述等。

#    write_info()：在软件包目录/opt/apps/<PKG_NAME>/下生成info文件，记录软件包的基本信息，如appid、名称、版本、架构、权限等。

#    write_json()：在软件包目录中生成app.json.update文件，记录软件包的详细信息，如名称、版本、文件名、种子地址、作者、贡献者、主页、更新时间、大小、更多信息、标签、截图地址和图标地址等。应当在商店目录使用而不是workdir

log.info "Please build the package here. Now support binary packaging."
log.info "Notice: Does not support dpkg-buildpackage now. Please build software from source manually or try shirodeb "

init_package_directory
write_control
write_info
dpkg-deb -Z xz -b . "$STORE_APP_PATH"/${PKG_NAME}_${VERSION}_${ARCH}.deb




popd

}
function pack-to-tar(){

pushd $STORE_APP_PATH
mkdir $CATAGORY/$PKG_NAME
mv app.json.update $CATAGORY/$PKG_NAME/app.json
mv "$STORE_APP_PATH"/${PKG_NAME}_${VERSION}_${ARCH}.deb $CATAGORY/$PKG_NAME/
tar -cvf $PKG_NAME.tar.gz $CATAGORY
mv $PKG_NAME.tar.gz $TAR_GZ_DIR
}

prepare
build
write_json $STORE_APP_PATH
pack-to-tar
rm -rf $WORKDIR



